﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SharedGamesClasses;
using System.Diagnostics;

namespace GuiGames {
    public partial class PigForm : Form {
        private Games MainGame;
        private string[] playersName = new string[Pig.NUM_OF_PLAYERS] { "Player", "Computer" };
        private int TURN = 0;
        private bool isFirst = true;
        private ListBoxTraceListener listBoxTraceListener;

        public PigForm(Games _game) {
            InitializeComponent();
            this.MainGame = _game;
            InitForm();
        }

        public void InitForm()
        {
            btnPlayAgain.Enabled = false;
            btnCancel.Enabled = false;
            listBoxTraceListener = new ListBoxTraceListener(pigListBox);
            Trace.Listeners.Add(listBoxTraceListener);
        }

        private void NextTurn()
        {
            TURN = TURN == 0 ? 1 : 0;
            isFirst = true;
            lblTurn.Text = TURN == 0 ? "Player" : "Computer";
        }

        protected override void OnClosed(EventArgs e)
        {
            MainGame.Show();
        }

        private void btnRollDie_Click(object sender, EventArgs e)
        {
            if (!btnCancel.Enabled)
                btnCancel.Enabled = true;

            if (!Pig.HasWon(0) && !Pig.HasWon(1))
            {
                if (!Pig.PlayGUI(TURN, isFirst))
                {
                    if (TURN == 0)
                    {
                        lblPlayerPoint.Text = (Pig.pointsTotal[TURN]).ToString();
                    }
                    else
                    {
                        lblComputerPoint.Text = (Pig.pointsTotal[TURN]).ToString();
                    }
                    NextTurn();
                }
                else
                {
                    if (TURN == 0)
                    {
                        lblPlayerPoint.Text = (Pig.pointsTotal[TURN] + Pig.pointsCurrent).ToString();
                    }
                    else
                    {
                        lblComputerPoint.Text = (Pig.pointsTotal[TURN] + Pig.pointsCurrent).ToString();
                    }

                    if ((Pig.pointsTotal[TURN] + Pig.pointsCurrent) < Pig.END_SQUARE)
                    {
                        DialogResult dialogResult = MessageBox.Show("Does the " + playersName[TURN] + " want to hold? (Yes or No)", // The question.
                                                    "Hold?", // The MessageBox's caption.
                                                     MessageBoxButtons.YesNo,
                                                     MessageBoxIcon.Question);

                        if (dialogResult == DialogResult.Yes)
                        {
                            Pig.pointsTotal[TURN] += Pig.pointsCurrent;
                            NextTurn();
                            isFirst = true;
                        }
                        else
                        {

                            isFirst = false;
                        }
                    }
                    else
                    {
                        btnRollDie.Enabled = false;
                        btnPlayAgain.Enabled = true;
                        MessageBox.Show(playersName[TURN] + " has won!\n Well Done.");
                    }
                }
            }
            else
            {
                if (Pig.HasWon(0))
                {
                    btnRollDie.Enabled = false;
                    btnPlayAgain.Enabled = true;
                    MessageBox.Show(playersName[0] + " has won!\n Well Done.");
                }
                else if (Pig.HasWon(1))
                {
                    btnRollDie.Enabled = false;
                    btnPlayAgain.Enabled = true;
                    MessageBox.Show(playersName[1] + " has won!\n Well Done.");
                }
            }
        }

        private void btnPlayAgain_Click(object sender, EventArgs e)
        {
            btnPlayAgain.Enabled = false;
            btnRollDie.Enabled = true;
            Pig.ResetGlobals();
            Pig.pointsTotal[0] = 0;
            Pig.pointsTotal[0] = 0;
            TURN = 0;
            isFirst = true;
            lblComputerPoint.Text = "0";
            lblPlayerPoint.Text = "0";
            lblTurn.Text = "Player";
            pigListBox.Items.Clear();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
